package com.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.Constant;
import com.android.scj.tc.Connection;
import com.android.scj.tc.TerminalService;
import com.android.scj.tc.TcAppFolw;
import com.android.scj.tc.bean.EerrBean;
import com.baseutils.SpfUtils;
import com.bean.TradeMessage;
import com.util.DelayTimeThread;

import java.io.IOException;
import java.util.NoSuchElementException;

/**
 * 激活OBU线程
 * 脱机
 *
 * @author 刘亚东
 */
@SuppressWarnings("unused")
public class CheckOBUThread extends Thread {

    // 设备业务类
    private static TerminalService terService = TerminalService.getService();
    // 通信类
    private static Connection con = null;


    private static TcAppFolw equiCon;

    private static String IsModifySignedDate = "";

    private static String SignedDate = "";

    private static String IsModifyExpiredDate = "";

    private static String ExpiredDate = "";
    public TradeMessage tradeMessage = new TradeMessage();
    public static Bundle bundle;

    public static String OBUMAC_JC;

    public static long time;

    public static boolean isDetectSucess = false;
    public static boolean isSucess = false;

    public static int nStep = 1;

    DelayTimeThread delaytime = null;
    Context context;
    String OBUMAC;
    Handler handler;

    public CheckOBUThread(Context context, String OBUMAC, Handler handler) {
        this.context = context;
        this.OBUMAC = OBUMAC;
        this.handler = handler;
        boolean psamopen = (boolean) SpfUtils.get(context, Constant.Check_Psam, true);
        equiCon = new TcAppFolw(context, psamopen);
        con = Connection.getConnection();
    }

    public void print(String tag, String context) {
        Log.e(tag, context);
    }


    public void activateOBU() throws Exception {
        //           1.OBU自检
        boolean cheack15 = (boolean) SpfUtils.get(context, Constant.Check_kl15, true);
        if (cheack15) {
            handler.sendEmptyMessage(7);
            String str = "";
            EerrBean eerrBean = equiCon.check15(OBUMAC);
            if (eerrBean.getCode() == 0) {
                String message1 = eerrBean.getMessage();
                String str1 = message1.substring(message1.indexOf(":") + 1);
                Log.e("test", "自检失败" + str1);
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage(str1);
                message.what = 1;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            } else {
                Log.e("test", "自检成功");
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage("OBU自检成功");
                message.what = 2;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            }
            Thread.sleep(1000);
        }

        //           2.检测蓝牙
        boolean Check_Bluetooth = (boolean) SpfUtils.get(context, Constant.Check_Bluetooth, true);
        if (Check_Bluetooth) {
            handler.sendEmptyMessage(8);
            EerrBean eerrBean = equiCon.Check_Bluetooth(OBUMAC);
            if (eerrBean.getCode() == 0) {
                Log.e("test", "检测蓝牙失败");
                String message1 = eerrBean.getMessage();
                String str1 = message1.substring(message1.indexOf(":") + 1);
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage(str1);
                message.what = 3;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            } else {
                Log.e("test", "检测蓝牙成功");
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage("OBU检测蓝牙成功");
                message.what = 4;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            }
            Thread.sleep(1000);
        }
        boolean Check_Vin = (boolean) SpfUtils.get(context, Constant.Check_Vin, true);
        if (Check_Vin) {
            handler.sendEmptyMessage(12);
            EerrBean eerrBean = equiCon.Vininput(OBUMAC);
            if (eerrBean.getCode() == 0) {
                Log.e("test", "VIn写入失败");
                String message1 = eerrBean.getMessage();
                String str1 = message1.substring(message1.indexOf(":") + 1);
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage(str1);
                message.what = 13;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            } else {
                Log.e("test", "VIn写入成功");
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage("Vin写入成功");
                message.what = 11;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            }
            Thread.sleep(1000);




        }
        handler.sendEmptyMessage(9);
        //          3.OBU交易
        int tradetime = (int) SpfUtils.get(context, "lsxtradenumber", 1);
        for (int i = 0; i < tradetime; i++) {
            EerrBean eerrBean = equiCon.Trade(OBUMAC);
            if (eerrBean.getCode() == 0) {
                String message1 = eerrBean.getMessage();
                String str1 = message1.substring(message1.indexOf(":") + 1);
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setMessage(str1);
                tradeMessage.setIndex("" + (i + 1));
                message.what = 5;
                message.obj = tradeMessage;
                handler.sendMessage(message);

            } else {
                Log.e("test", "OBU交易成功");
                Message message = new Message();
                tradeMessage.setMac(equiCon.GetOBUMAC());
                tradeMessage.setIndex("" + (i + 1));
                message.what = 6;
                message.obj = tradeMessage;
                handler.sendMessage(message);
            }


            Thread.sleep(1000);
        }

    }

    private void JhLoadBundMess(int nType) throws Exception {
        bundle = new Bundle();
        bundle.putString("OBUMAC", equiCon.GetOBUMAC());
        bundle.putString("OBUSERIALNUMBER", equiCon.GetOBUSerialNumber());
        bundle.putString("VLP", equiCon.VLP);
        if (nType == 0) {
            bundle.putString("VLP", "" + equiCon.VLP);
            bundle.putString("VLPC", "" + equiCon.VLPC);
            bundle.putString("VCLASS", "" + equiCon.VClass);
//			bundle.putString("VWEIGHT", ""+equiCon.numberofpeople_VWeight);
        }
        bundle.putString("CARDVLP", equiCon.GetCardVlp());
        bundle.putString("CARDVLPC", equiCon.GetCardVehicleColor());
        bundle.putString("CARDSERIALNUMBER", equiCon.GetCardSerialNumber());
        bundle.putString("CARDTYPE", equiCon.GetCardType());
        bundle.putString("PSAMSN", equiCon.PsamIdNumber);
    }

    public int detection() throws NoSuchElementException,
            NumberFormatException, IOException, Exception {

        time = System.currentTimeMillis();

        equiCon.SendBst();
        try {
            equiCon.SendGetSecure2((int) 0x4D, TcAppFolw.DecryptKeyType);
        } catch (Exception e) {
            String strE = e.getMessage();
            if (strE.equals("GetSecure失败！"))
                strE = "读OBU超时，请重试!";
            else if (strE.equals("非GetSecureRs！"))
                strE = "读OBU异常数据，请重试!";
            else if (strE.equals("GetSecureRs错误！"))
                strE = "读OBU失败,请确认OBU!";
            else
                strE = "读OBU失败," + e.getMessage();
            throw new NumberFormatException(strE);
        }
        try {
            equiCon.SendSetMMi("00");
            equiCon.SendEventReport();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private void JcLoadBundMess() throws Exception {
        OBUMAC_JC = equiCon.GetOBUMAC();
        bundle = new Bundle();
        bundle.putString("OBUMAC", equiCon.GetOBUMAC());
        bundle.putString("OBUSERIALNUMBER", equiCon.GetOBUSerialNumber());
        bundle.putString("VLP", "" + equiCon.VLP);
        bundle.putString("VLPC", "" + equiCon.VLPC);
        bundle.putString("VCLASS", "" + equiCon.VClass);
//		bundle.putString("VWEIGHT", ""+equiCon.mberofpeople_VWeight);
        bundle.putString("CARDVLP", equiCon.GetCardVlp());
        bundle.putString("CARDVLPC", equiCon.GetCardVehicleColor());
        bundle.putString("OBUSTATUS", equiCon.GetObuStatus());
        bundle.putString("OBUBATTERY", equiCon.GetObuBattery());
        bundle.putString("OBUDATE", TcAppFolw.convertDate(equiCon.GetObuDate().substring(0, 8)));
        bundle.putString("OBUDATE2", TcAppFolw.convertDate(equiCon.GetObuDate().substring(8, 16)));
        bundle.putString("CARDSERIALNUMBER", equiCon.GetCardSerialNumber());
        bundle.putString("CARDTYPE", equiCon.GetCardType());
        bundle.putString("CARDBALANCE", equiCon.GetCardBalance());
        bundle.putString("CARDDATE", equiCon.GetCardDate1());
        bundle.putString("CARDDATE2", equiCon.GetCardDate2());
        String cardSerialNumber = equiCon.GetCardSerialNumber();
        String cardNetNumber = equiCon.GetCardNetNumber();
        Log.d("CZLog", "cardSerialNumber = " + cardSerialNumber + " , cardNetNumber = " + cardNetNumber);
        //	WriteStreamAppend.appEnd("cardSerialNumber = " + cardSerialNumber + " , cardNetNumber = " + cardNetNumber);
    }


    @Override
    public void run() {
        try {
            activateOBU();
//            JhLoadBundMess(0);
            Log.e("zzz", "OBU检测成功");
            Log.e("test", "OBU完成");
            Message message = new Message();
            message.what = 10;
            handler.sendMessage(message);
        } catch (Exception e) {
            Log.e("zzz", "OBU检测失败:" + e.getMessage());
            Message message = new Message();
            message.what = 10;
            handler.sendMessage(message);
//            try {
//                JhLoadBundMess(1);
//            } catch (Exception e1) {
//                e1.printStackTrace();
//            }


        }
    }
}
